package com.xckj.cycling.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;

import com.xckj.cycling.R;

/**
 * @Author: ChenYao
 * @Describe: 性别的Dialog
 * @Date: 2023/3/2 22:44
 */
public class GenderDialog {
    private final Context context;
    private final int themeResId;
    private final View layout;
    private int gender = -1;
    private SelectedGenderOnClickListener selectedGenderOnClickListener;


    public GenderDialog(Context context) {
        this(context, R.style.GenderDialogStyle);
    }

    @SuppressLint("InflateParams")
    public GenderDialog(Context context, int themeResId) {
        this(context, themeResId, ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_dialog_gender, null));
    }

    /**
     * 自定义layout用这个
     */
    public GenderDialog(Context context, int themeResId, View layout) {
        this.context = context;
        this.themeResId = themeResId;
        this.layout = layout;
    }

    public GenderDialog setSelectedGenderClickListener(SelectedGenderOnClickListener selectedGenderOnClickListener) {
        this.selectedGenderOnClickListener = selectedGenderOnClickListener;
        return this;
    }


    @SuppressLint("NonConstantResourceId")
    public Dialog build() {
        final Dialog dialog = new Dialog(context, themeResId);
        dialog.setCancelable(false);
        dialog.addContentView(layout, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
        ImageView ivClose = dialog.findViewById(R.id.iv_close);
        RadioGroup rgGender = dialog.findViewById(R.id.rg_gender);
        TextView btnSubmit = dialog.findViewById(R.id.btn_submit);
        ivClose.setOnClickListener(view -> {
            dialog.dismiss();
        });
        rgGender.setOnCheckedChangeListener((radioGroup, i) -> {
            switch (i) {
                case R.id.rb_gender_man:
                    gender = 1;
                    break;
                case R.id.rb_gender_women:
                    gender = 2;
                    break;
                case R.id.rb_gender_other:
                    gender = 3;
                    break;
                default:
                    break;
            }
        });
        btnSubmit.setOnClickListener(view -> {
            if (null != selectedGenderOnClickListener) {
                if (-1 == gender) {
                    Toast.makeText(context, context.getString(R.string.gender_null_hint), Toast.LENGTH_SHORT).show();
                    return;
                }
                selectedGenderOnClickListener.onSelectedGender(gender);
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public interface SelectedGenderOnClickListener {
        /**
         * 选择完性别后回调
         *
         * @param gender 性别 1:男 2:女 3:未知 -1:未选
         */
        void onSelectedGender(int gender);
    }
}
